package com.example.myapplication.slice;

import com.example.common.base.BaseAbilitySlice;
import com.example.common.dialog.ToastUtil;
import com.example.myapplication.ResourceTable;
import com.example.myapplication.ability.WebAbility;
import com.example.myapplication.contract.ILoginContract;
import com.example.myapplication.presenter.LoginPresenter;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class LoginAbilitySlice extends BaseAbilitySlice implements ILoginContract.View, Component.ClickedListener {
    private ILoginContract.Presenter presenter;
    private TextField tfInputPhoneNumber;
    private TextField tfInputVerifyCode;
    private Button btnGetVerifyCode;
    private Image imgClear;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        presenter = new LoginPresenter(this);
        initComponent();
    }

    private void initComponent() {
        findComponentById(ResourceTable.Id_iv_back).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_login).setClickedListener(this);
        findComponentById(ResourceTable.Id_tv_user_protocol).setClickedListener(this);
        findComponentById(ResourceTable.Id_tv_user_secret).setClickedListener(this);
        btnGetVerifyCode = (Button) findComponentById(ResourceTable.Id_btn_get_verify_code);

        imgClear = (Image) findComponentById(ResourceTable.Id_img_clear_verify_code);
        btnGetVerifyCode.setClickedListener(this);
        tfInputPhoneNumber = (TextField) findComponentById(ResourceTable.Id_tf_input_phone_num);
        tfInputVerifyCode = (TextField) findComponentById(ResourceTable.Id_tf_input_verify_code);
        tfInputPhoneNumber.addTextObserver((s, i, i1, i2) -> {
            tfInputPhoneNumber.setText(s.length() > 11 ? s.substring(0, 12) : s);
            btnGetVerifyCode.setEnabled(s.length() == 11);
        });
        tfInputVerifyCode.addTextObserver((s, i, i1, i2) -> imgClear.setVisibility(s.length() > 0 ? Component.VISIBLE : Component.INVISIBLE));
    }

    @Override
    public void onClick(Component component) {
        Intent intent;
        Operation operation;
        switch (component.getId()) {
            case ResourceTable.Id_iv_back:
                onBackPressed();
                break;
            case ResourceTable.Id_tv_user_protocol:
                intent = new Intent();
                intent.setParam("title", "用户服务协议");
                intent.setParam("url", "https://icbc.jdd966.com/icbcETCh5/protocol-frm.html");
                present(new WebAbilitySlice(), intent);
                break;
            case ResourceTable.Id_tv_user_secret:
                operation = new Intent.OperationBuilder()
                        .withBundleName("com.example.myapplication")
                        .withAbilityName(WebAbility.class.getCanonicalName())
                        .build();
                intent = new Intent();
                intent.setOperation(operation);
                intent.setParam("title", "用户隐私政策");
                intent.setParam("url", "https://icbc.jdd966.com/icbcETCh5/privacyPolicy-frm.html");
                startAbility(intent);
                break;
            case ResourceTable.Id_btn_get_verify_code:
                presenter.getPhoneVerifyCode(tfInputPhoneNumber.getText());
                break;
            case ResourceTable.Id_btn_login:
                presenter.login(tfInputPhoneNumber.getText(), tfInputVerifyCode.getText());
                break;
            default:
                break;
        }
    }

    @Override
    public void showToast(String msg) {
        new ToastUtil(this, msg).show();
    }

    @Override
    public void showNewAccountDialog(String phoneStr) {
    }

    private void log(String str) {
        HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00201, LoginAbilitySlice.class.getSimpleName());
        HiLog.error(hiLogLabel, str);
    }

}
